package com.shop.admin.controller;

import com.shop.admin.aop.Log;
import com.shop.admin.service.IQuestionService;
import com.shop.admin.validate.commons.IdValidate;
import com.shop.admin.validate.question.QuestionCreateValidate;
import com.shop.admin.validate.question.QuestionUpdateValidate;
import com.shop.admin.validate.question.QuestionSearchValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.vo.question.QuestionListedVo;
import com.shop.admin.vo.question.QuestionDetailVo;
import com.shop.common.core.AjaxResult;
import com.shop.common.core.PageResult;
import com.shop.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("${app.api-prefix}/question")
@Api(tags = "试题管理")
public class QuestionController {

    @Resource
    IQuestionService iQuestionService;

    @GetMapping("/list")
    @ApiOperation(value="试题列表")
    public AjaxResult<PageResult<QuestionListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated QuestionSearchValidate searchValidate) {
        PageResult<QuestionListedVo> list = iQuestionService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="试题详情")
    public AjaxResult<QuestionDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        QuestionDetailVo detail = iQuestionService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "试题新增")
    @PostMapping("/add")
    @ApiOperation(value="试题新增")
    public AjaxResult<Object> add(@Validated @RequestBody QuestionCreateValidate createValidate) {
        iQuestionService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "试题编辑")
    @PostMapping("/edit")
    @ApiOperation(value="试题编辑")
    public AjaxResult<Object> edit(@Valid @RequestBody QuestionUpdateValidate updateValidate) {
        iQuestionService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "试题删除")
    @PostMapping("/del")
    @ApiOperation(value="试题删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iQuestionService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
